import { URL, ajax, Toast, getUser } from '../../utils/index.js'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        user: {},
        fileList: [],
        errorNameMessage: '',
        errorPhoneMessage: '',
    },
    setUser(data) {
        this.setData({
            user: data
        })
    },
    image_up() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: (res) => {
                if (res.errMsg == "chooseImage:ok") {
                    this.imgUploadRequest(res.tempFilePaths[0])
                }
            }
        })
    },
    input(e) {
        let userkey = 'user.' + e.currentTarget.dataset.inputname
        this.setData({
            [userkey]: e.detail
        })
    },
    btnsubmit() {
        let { realname, phone, head, oaCode, errorNameMessage, errorPhoneMessage } = this.data.user, boo = false, reg = new RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/);
        !realname ? errorNameMessage = '请输入姓名' : errorNameMessage = '', !phone ? errorPhoneMessage = '请输入电话号码' : !reg.test(phone) ? errorPhoneMessage = '电话号码格式错误' : (boo = true, errorPhoneMessage = ''), this.setData({ errorPhoneMessage, errorNameMessage })
        if (!boo || !realname || !phone) return
        ajax.loading(), ajax.post('/applet/user/updateUserInfo', { realname, phone, head, oaCode }).then(res => res.data.code == 1 ? (
            //Toast.success(res.data.msg),)
            Toast({
                type: 'success',
                forbidClick: true,
                message: res.data.msg,
                duration:1000,
                onClose: () => (wx.navigateBack({delta:1}))
              })

            ) : (ajax.toast.fail(res.data.msg), reject(res))).catch(e => (ajax.toast.fail('接口错误updateUserInfo'), reject(e), console.log(e)));
    },
    // 上传图片接口
    imgUploadRequest(file) {
        ajax.loading()
        wx.uploadFile({
            url: 'https://www.zl-ai.com/applet/user/updateHeadImg',
            filePath: file,
            name: 'file',
            success: (res) => {
                let data = JSON.parse(res.data)
                if (data.code == '1') {
                    this.setData({
                        'user.head': data.data.head
                    }), Toast.success(data.msg)
                } else Toast.fail(data.msg)
            },
            fail: (re) => {
                console.log(e), reject(e), Toast.clear()
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        getUser().then(res => this.setUser(res.data.data))
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})